<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="往来类别" prop="type">

      <el-input v-model="dataForm.type" placeholder="类别"></el-input>
    </el-form-item>
    <el-form-item label="订单ID" prop="orderDetailId">
      <el-input v-model="dataForm.orderDetailId" placeholder="订单ID"></el-input>
    </el-form-item>
    <el-form-item label="订单编号" prop="orderDetailNo">
      <el-input v-model="dataForm.orderDetailNo" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="出库单编号" prop="productLeaveStorageId">
      <el-input v-model="dataForm.productLeaveStorageId" placeholder="出库单编号"></el-input>
    </el-form-item>
    <el-form-item label="出库单编号" prop="productLeaveStorageNo">
      <el-input v-model="dataForm.productLeaveStorageNo" placeholder="出库单编号"></el-input>
    </el-form-item>
    <el-form-item label="客户合同编号" prop="clientContractNo">
      <el-input v-model="dataForm.clientContractNo" placeholder="客户合同编号"></el-input>
    </el-form-item>
    <el-form-item label="单据时间" prop="dayTime">
      <el-input v-model="dataForm.dayTime" placeholder="单据时间"></el-input>
    </el-form-item>
    <el-form-item label="产品名称ID" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="产品名称ID"></el-input>
    </el-form-item>
    <el-form-item label="产品名称" prop="productNo">
      <el-input v-model="dataForm.productNo" placeholder="产品名称"></el-input>
    </el-form-item>
    <el-form-item label="销售数量" prop="realSaleNumber">
      <el-input v-model="dataForm.realSaleNumber" placeholder="销售数量"></el-input>
    </el-form-item>
    <el-form-item label="单价" prop="unitPrice">
      <el-input v-model="dataForm.unitPrice" placeholder="单价"></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="金额"></el-input>
    </el-form-item>
    <el-form-item label="折扣" prop="discount">
      <el-input v-model="dataForm.discount" placeholder="折扣"></el-input>
    </el-form-item>
    <el-form-item label="折扣单价" prop="discountUnitPrice">
      <el-input v-model="dataForm.discountUnitPrice" placeholder="折扣单价"></el-input>
    </el-form-item>
    <el-form-item label="期初金额" prop="openingBalanceAmount">
      <el-input v-model="dataForm.openingBalanceAmount" placeholder="期初金额"></el-input>
    </el-form-item>
    <el-form-item label="应收余额" prop="balanceReceivableAmount">
      <el-input v-model="dataForm.balanceReceivableAmount" placeholder="应收余额"></el-input>
    </el-form-item>
    <el-form-item label="财务收款ID" prop="financeOughtReceiveId">
      <el-input v-model="dataForm.financeOughtReceiveId" placeholder="财务收款ID"></el-input>
    </el-form-item>
    <el-form-item label="客户名称ID" prop="clientId">
      <el-input v-model="dataForm.clientId" placeholder="客户名称ID"></el-input>
    </el-form-item>
    <el-form-item label="客户名称" prop="clientName">
      <el-input v-model="dataForm.clientName" placeholder="客户名称"></el-input>
    </el-form-item>
    <el-form-item label="克重" prop="productWeight">
      <el-input v-model="dataForm.productWeight" placeholder="克重"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="附加说明" prop="additionalRemark">
      <el-input v-model="dataForm.additionalRemark" placeholder="附加说明"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateUser">
      <el-input v-model="dataForm.updateUser" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          type: '',
          orderDetailId: '',
          orderDetailNo: '',
          productLeaveStorageId: '',
          productLeaveStorageNo: '',
          clientContractNo: '',
          dayTime: '',
          productId: '',
          productNo: '',
          realSaleNumber: '',
          unitPrice: '',
          totalPrice: '',
          discount: '',
          discountUnitPrice: '',
          openingBalanceAmount: '',
          balanceReceivableAmount: '',
          financeOughtReceiveId: '',
          clientId: '',
          clientName: '',
          productWeight: '',
          remark: '',
          additionalRemark: '',
          status: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: ''
        },
        dataRule: {
          type: [
            { required: true, message: '类别不能为空', trigger: 'blur' }
          ],
          orderDetailId: [
            { required: true, message: '订单ID不能为空', trigger: 'blur' }
          ],
          orderDetailNo: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          productLeaveStorageId: [
            { required: true, message: '出库单编号不能为空', trigger: 'blur' }
          ],
          productLeaveStorageNo: [
            { required: true, message: '出库单编号不能为空', trigger: 'blur' }
          ],
          clientContractNo: [
            { required: true, message: '客户合同编号不能为空', trigger: 'blur' }
          ],
          dayTime: [
            { required: true, message: '单据时间不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '产品名称ID不能为空', trigger: 'blur' }
          ],
          productNo: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          realSaleNumber: [
            { required: true, message: '销售数量不能为空', trigger: 'blur' }
          ],
          unitPrice: [
            { required: true, message: '单价不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '金额不能为空', trigger: 'blur' }
          ],
          discount: [
            { required: true, message: '折扣不能为空', trigger: 'blur' }
          ],
          discountUnitPrice: [
            { required: true, message: '折扣单价不能为空', trigger: 'blur' }
          ],
          openingBalanceAmount: [
            { required: true, message: '期初金额不能为空', trigger: 'blur' }
          ],
          balanceReceivableAmount: [
            { required: true, message: '应收余额不能为空', trigger: 'blur' }
          ],
          financeOughtReceiveId: [
            { required: true, message: '财务收款ID不能为空', trigger: 'blur' }
          ],
          clientId: [
            { required: true, message: '客户名称ID不能为空', trigger: 'blur' }
          ],
          clientName: [
            { required: true, message: '客户名称不能为空', trigger: 'blur' }
          ],
          productWeight: [
            { required: true, message: '克重不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          additionalRemark: [
            { required: true, message: '附加说明不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/finance/financetransactionproduct/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.financeTransactionProduct.type
                this.dataForm.orderDetailId = data.financeTransactionProduct.orderDetailId
                this.dataForm.orderDetailNo = data.financeTransactionProduct.orderDetailNo
                this.dataForm.productLeaveStorageId = data.financeTransactionProduct.productLeaveStorageId
                this.dataForm.productLeaveStorageNo = data.financeTransactionProduct.productLeaveStorageNo
                this.dataForm.clientContractNo = data.financeTransactionProduct.clientContractNo
                this.dataForm.dayTime = data.financeTransactionProduct.dayTime
                this.dataForm.productId = data.financeTransactionProduct.productId
                this.dataForm.productNo = data.financeTransactionProduct.productNo
                this.dataForm.realSaleNumber = data.financeTransactionProduct.realSaleNumber
                this.dataForm.unitPrice = data.financeTransactionProduct.unitPrice
                this.dataForm.totalPrice = data.financeTransactionProduct.totalPrice
                this.dataForm.discount = data.financeTransactionProduct.discount
                this.dataForm.discountUnitPrice = data.financeTransactionProduct.discountUnitPrice
                this.dataForm.openingBalanceAmount = data.financeTransactionProduct.openingBalanceAmount
                this.dataForm.balanceReceivableAmount = data.financeTransactionProduct.balanceReceivableAmount
                this.dataForm.financeOughtReceiveId = data.financeTransactionProduct.financeOughtReceiveId
                this.dataForm.clientId = data.financeTransactionProduct.clientId
                this.dataForm.clientName = data.financeTransactionProduct.clientName
                this.dataForm.productWeight = data.financeTransactionProduct.productWeight
                this.dataForm.remark = data.financeTransactionProduct.remark
                this.dataForm.additionalRemark = data.financeTransactionProduct.additionalRemark
                this.dataForm.status = data.financeTransactionProduct.status
                this.dataForm.createTime = data.financeTransactionProduct.createTime
                this.dataForm.createUser = data.financeTransactionProduct.createUser
                this.dataForm.updateTime = data.financeTransactionProduct.updateTime
                this.dataForm.updateUser = data.financeTransactionProduct.updateUser
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/finance/financetransactionproduct/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'orderDetailId': this.dataForm.orderDetailId,
                'orderDetailNo': this.dataForm.orderDetailNo,
                'productLeaveStorageId': this.dataForm.productLeaveStorageId,
                'productLeaveStorageNo': this.dataForm.productLeaveStorageNo,
                'clientContractNo': this.dataForm.clientContractNo,
                'dayTime': this.dataForm.dayTime,
                'productId': this.dataForm.productId,
                'productNo': this.dataForm.productNo,
                'realSaleNumber': this.dataForm.realSaleNumber,
                'unitPrice': this.dataForm.unitPrice,
                'totalPrice': this.dataForm.totalPrice,
                'discount': this.dataForm.discount,
                'discountUnitPrice': this.dataForm.discountUnitPrice,
                'openingBalanceAmount': this.dataForm.openingBalanceAmount,
                'balanceReceivableAmount': this.dataForm.balanceReceivableAmount,
                'financeOughtReceiveId': this.dataForm.financeOughtReceiveId,
                'clientId': this.dataForm.clientId,
                'clientName': this.dataForm.clientName,
                'productWeight': this.dataForm.productWeight,
                'remark': this.dataForm.remark,
                'additionalRemark': this.dataForm.additionalRemark,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'createUser': this.dataForm.createUser,
                'updateTime': this.dataForm.updateTime,
                'updateUser': this.dataForm.updateUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
